//
//  MonitorController.m
//  Stock
//
//  Created by blacksun on 11-7-6.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MonitorController.h"


@implementation MonitorController
@synthesize table1;
@synthesize table2;
@synthesize table1Data;
@synthesize table2Data;
@synthesize monitorService;
@synthesize stockMonitor;
@synthesize updateTimeLabel;
@synthesize container;

#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    NSArray *array = aResult;
    self.stockMonitor = array.count>0?[array objectAtIndex:0]:nil;
    [self.table1 reloadData];
    
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}
- (void)dealloc
{
    self.table1 = nil;
    self.table2 = nil;
    self.updateTimeLabel = nil;
    self.container = nil;
    self.table1Data = nil;
    self.table2Data = nil;
    [self.monitorService cancleAllRequest];
    self.monitorService = nil;
    self.stockMonitor = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.table2Data = [NSArray arrayWithObjects:@"当日个股资金流向监控",@"3日个股资金流向监控",@"10个股日资金流向监控",@"60日个股资金流向监控",nil];
    self.table1Data = [NSArray arrayWithObjects:@"沪市A股",@"深市A股",@"沪深300", nil];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.monitorService = [[[StockMonitorService alloc] initWithDelegate:self] autorelease];
    [monitorService querySTAT];
    
    if ([StockContext shareInstance].updateTimes) {
        self.container.frame = CGRectMake(0.0, 30.0, 320.0, 416.0);
        self.updateTimeLabel.hidden = NO;
        NSString *fmt = @"";
        for (NSDictionary *dict in [StockContext shareInstance].updateTimes) {
            if ([@"FMT" isEqualToString:[dict objectForKey:@"id"]]) {
                fmt = [dict objectForKey:@"time"];
            }
        }
        updateTimeLabel.text = [updateTimeLabel.text stringByAppendingString:fmt];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelay:1.0];
        [UIView setAnimationDuration:0.5];
        self.updateTimeLabel.frame = CGRectMake(0.0, -30.0, 320.0, 30.0);
        self.container.frame = CGRectMake(0.0, 0.0, 320.0, 416.0);
        [UIView commitAnimations]; 
    }
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.table1 = nil;
    self.table2 = nil;
    self.updateTimeLabel = nil;
    self.container = nil;
    self.table1Data = nil;
    self.table2Data = nil;
    [self.monitorService cancleAllRequest];
    self.monitorService = nil;
    self.stockMonitor = nil;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == table1) {
     return [table1Data count];
    }
    return [table2Data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"QuotationCenterCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if (tableView == table1) {
            cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg1.png"]] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            
            UIColor *clearColor = [UIColor clearColor];
            
            UILabel *changeRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(160.0, 12.0, 150.0, 20.0)];
            changeRateLabel.backgroundColor = clearColor;
            changeRateLabel.tag = kStockMonitorHS300Tag;
            changeRateLabel.textColor = [UIColor whiteColor];
            changeRateLabel.textAlignment = UITextAlignmentRight;
            
            [cell.contentView addSubview:changeRateLabel];
            [changeRateLabel release];
            
            
        }else{
            cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg2.png"]] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:17.0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (tableView == table2) {
        cell.textLabel.text = [self.table2Data objectAtIndex:indexPath.row];
    }else{
        
        /*StockMonitor *currentMonitor = [table1Data objectAtIndex:indexPath.row];
        UILabel *itemLabel = (UILabel *)[cell viewWithTag:kStockMonitorTitleTag];
        UILabel *shLabel = (UILabel *)[cell viewWithTag:kStockMonitorSHTag];
        UILabel *szLabel = (UILabel *)[cell viewWithTag:kStockMonitorSZTag];
        */
        cell.textLabel.text = [table1Data objectAtIndex:indexPath.row];
        UILabel *rightLabel = (UILabel *)[cell viewWithTag:kStockMonitorHS300Tag];
        if (indexPath.row==0) {
            rightLabel.textColor = [stockMonitor.sh hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
            rightLabel.text = stockMonitor.sh;
        }else if(indexPath.row==1){
            rightLabel.textColor = [stockMonitor.sz hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
            rightLabel.text = stockMonitor.sz;
        }else if(indexPath.row==2){
            rightLabel.textColor = [stockMonitor.hs300 hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
            rightLabel.text = stockMonitor.hs300;
        }
        
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == table2) {
        if (indexPath.row==0) {
            [StockContext shareInstance].workingStock = 9;
        }else if(indexPath.row==1){
            [StockContext shareInstance].workingStock = 10;
        }else if(indexPath.row==2){
            [StockContext shareInstance].workingStock = 11;
        }else if(indexPath.row==3){
            [StockContext shareInstance].workingStock = 12;
        }
        ItemListController *childController = [[ItemListController alloc] initWithNibName:@"ItemListController" bundle:nil];
        childController.title = [table2Data objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:childController animated:YES];
        [childController release];
    }
}

@end
